package engine.ui
{
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	
	import newx.StageInfo;
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.control.button.NxButton;
	import newx.com.control.window.NxWindow;
	import newx.com.control.window.NxWindowDragType;
	import newx.utils.DrawAPI;
	
	public class AlertUI extends NxWindow
	{
		public	static const 	YES			:int	=	0x1000;
		public	static const 	NO			:int	=	0x0100;
		public	static const 	OK			:int	=	0x0010;
		public	static const 	CANCEL		:int	=	0x0001;

		
		private static var notePanel			:AlertUI;
		private static var hasConstruct		:Boolean		=	false;
		private static var _stage				:Stage;
		
		/**按钮间的距离*/
		private var _distace			:uint	=	10;
		
		private var tbnSkinStr	:String		=	"";
		private static var txtSkinNormal	:String		=	"";
		private static var txtSkinInput	:String		=	"";
		
		private var _buttonSpace	:Sprite;
		
		private var _buttonMode	:uint;
		
		private var inputTxt				:TextField;
		
		private var txtNormal		:TextField;
		private var _normalSpace	:Sprite;
		private var _inputSpace	:Sprite;
		
		
		/**四种样式的按钮*/
		private var firstBtn			:NxButton;
		private var secondBtn			:NxButton;
		private var thirdBtn			:NxButton;
		private var fouthBtn			:NxButton;
		
		private var callBackFn			:Function;
		private var _clickData			:Array;
		
		private var _isHideMsg			:Boolean = false;
		
		private var _height:int;
		
		
		public function AlertUI(windowSkinClass	:String,
								btnSkinClass	:String,
								txtSkin			:String,
								w		:uint	=	300,
								h		:uint	=	150)
		{
			super();
			
			this.title			=	"";
			this.width			=	w;
			this.height			=	h;
			this.skin			=	windowSkinClass;
			this.dragSpaceType	=	""
			titleIcon = "tishi";
			if(AlertUI._stage || AlertUI.hasConstruct)
			{
				throw new Error(" ------- AlertEx 已经在内存中存在，不能重复构建 AlertEx -------");
				return;
			}
			
			tbnSkinStr	=	btnSkinClass;
			txtSkinInput	=	txtSkin;
			
			AlertUI.hasConstruct	=	true;
			this.visible			=	false;
			this.closeBtnVisible	=	false;
			initUI();
		}
		
		public static function Init(_client:DisplayObject,
										windowSkinClass	:String,
										btnSkinClass	:String,
										txtSkin			:String,
										w		:uint	=	300,
										h		:uint	=	150):void
		{
			
			if(!AlertUI._stage)
			{
				if(_client.stage)
				{
					AlertUI.notePanel		=	new AlertUI(windowSkinClass,btnSkinClass,txtSkin,w,h);
					AlertUI._stage		=	_client.stage;
					AlertUI.notePanel.show(AlertUI._stage);
				}
				else
				{
					throw new Error("初始化失败, " + _client + " 不在显示列表中，请先将 " +
						_client + " 添加到显示列表中!");
				}
			}
		}
		
		
		private function initUI(...argc):void
		{
			_normalSpace = new Sprite();
			_normalSpace.mouseChildren	=	false;
			_normalSpace.mouseEnabled	=	false;
			_normalSpace.x	=	width/9;
			_normalSpace.y	=	height/5;
			addChild(_normalSpace);
			
			color = 0xBE9C71;
			
			inputTxt	=	new TextField();
			inputTxt.type	=	TextFieldType.INPUT;
			txtNormal	=	new TextField();
			txtNormal.type		=	TextFieldType.DYNAMIC;
			txtNormal.multiline	=	true;
			txtNormal.wordWrap	=	true;
			txtNormal.textColor	=	0xBE9C71;
			txtNormal.background	=	false;
			var glow:GlowFilter		= 	new GlowFilter();
			glow.color 				= 	0x000000;
			glow.alpha 				= 	1;
			glow.blurX 				=	2;
			glow.blurY				= 	2;
			glow.strength			=	4;
			glow.quality			= 	BitmapFilterQuality.HIGH;
			txtNormal.filters		=	[glow];
			
			//init skin
			_inputSpace	=	new Sprite();
			var inputSkin:Sprite	=	ResManager.newResClass(txtSkinInput) as Sprite;
			inputSkin.width	= width*.5;	
			inputSkin.height	= 26;	
			inputTxt.width	=	height - 5;
			inputTxt.height	=	25;
			inputTxt.x	=	3;
			inputTxt.y	=	3;
			inputTxt.textColor	=	0xBE9C71;
			_inputSpace.addChild(inputSkin);
			_inputSpace.addChild(inputTxt);
			_inputSpace.x	=	width/4;
			_inputSpace.y	=	height/3;
			addChild(_inputSpace);
			creatAlert();
		}
		
		private function creatAlert():void
		{
			_buttonSpace	=	new Sprite();
			
			firstBtn	=	new NxButton();
			secondBtn	=	new NxButton();
			thirdBtn	=	new NxButton();
			fouthBtn	=	new NxButton();
			
			firstBtn.label		=	secondBtn.label		=	
				thirdBtn.label		=	fouthBtn.label		=	"";
			
			firstBtn.skinWidth	=	secondBtn.skinWidth	=	
				thirdBtn.skinWidth	=	fouthBtn.skinWidth	=	70;
			
			firstBtn.skinHeight	=	secondBtn.skinHeight	=	
				thirdBtn.skinHeight	=	fouthBtn.skinHeight	=	28;
			
			firstBtn.skin	=	secondBtn.skin	=	
				thirdBtn.skin	=	fouthBtn.skin	=	tbnSkinStr;
			
		//	firstBtn.left	=	secondBtn.left	=	thirdBtn.left	=	fouthBtn.left	= 15;
		//	firstBtn.top	=	secondBtn.top	=	thirdBtn.top	=	fouthBtn.top	= 0;
		//	firstBtn.textLineSize	=	secondBtn.textLineSize	=	thirdBtn.textLineSize	=	fouthBtn.textLineSize	= 2;
		//	firstBtn.textLineColor	=	secondBtn.textLineColor	=	thirdBtn.textLineColor	=	fouthBtn.textLineColor	= 0X000000;
			
	//		firstBtn.autoSize	=	secondBtn.autoSize	=	thirdBtn.autoSize	=	fouthBtn.autoSize	=	false;
				
			addChild(_buttonSpace);
		}
		
		/**
 		 *确定buttonSpace在窗体中的坐标 
 		 */		
		private function setButtonXY():void
		{
			_buttonSpace.x	=	Math.floor((width	-	_buttonSpace.width)	*	0.5);
			_buttonSpace.y	=	_height		-	_buttonSpace.height	-	28;
		}
		
		private function deleteEvent():void
		{
			firstBtn.removeEventListener(MouseEvent.CLICK, onButtonClick);
			secondBtn.removeEventListener(MouseEvent.CLICK, onButtonClick);
			
			if( inputTxt.hasEventListener( MouseEvent.CLICK) )
			{
				inputTxt.removeEventListener( MouseEvent.CLICK, onInputTextClick );
			}
		}
		
		public static function showAlert(titleStr		:String,
									messageStr		:String,	
									buttonMode		:int		=	AlertUI.OK,
									clickCallBack	:Function	=	null,
									clickData		:Array		=	null,									
									isInput		:Boolean		= 	false,
									w:uint=300,h:uint=150,
									initStr:String ="",
									restrictStr:String = null,
									firstLabel:String=null,
									secondLabel:String=null):void
		{
			if(!AlertUI._stage)
			{
				AlertUI.Init(StageInfo.stage,"Panel_1","Button_2","textSkin");
			}
			AlertUI.notePanel._isHideMsg = false;
			AlertUI.notePanel.width	=	w;
			AlertUI.notePanel.height	=	h;
			AlertUI.notePanel.showPanel(titleStr,messageStr,buttonMode,clickCallBack,clickData,isInput,initStr,restrictStr,firstLabel,secondLabel);
		}
		
		public static  function hideAlert():void
		{
			if(AlertUI.notePanel != null)
				AlertUI.notePanel.hideAlertEx();
		}
		
		public function hideAlertEx():void
		{
			this.visible	=	false;
		}
		
		/**
		 * 
		 * @param titleStr
		 * @param messageStr
		 * @param buttonStr
		 * @param buttonMode
		 * @param clickCallBack
		 * @param clickData
		 * @param isInput
		 * @param w
		 * @param h
		 * @param initStr
		 * @param isHideMsg	当有提示文本的时候并且是true的时候，鼠标移动上去，删除提示
		 * 
		 */
		public static function showAlertEx(titleStr		:String,
										 messageStr		:String,	
										 buttonStr		:Array,
										 buttonMode		:int		=	AlertUI.OK,
										 clickCallBack	:Function	=	null,
										 clickData		:Array		=	null,									
										 isInput		:Boolean		= 	false,w:uint=300,h:uint=150,initStr:String ="", isHideMsg : Boolean =false ):void
		{
			if(!AlertUI._stage)
			{
				AlertUI.Init(StageInfo.stage,"Panel_1","Button_2","textSkin");
			}
			AlertUI.notePanel.width	=	w;
			AlertUI.notePanel.height	=	h;
			AlertUI.notePanel._isHideMsg = isHideMsg;
			AlertUI.notePanel.showPanelEx(titleStr,messageStr,buttonStr,buttonMode,clickCallBack,clickData,isInput,initStr);
		}
		
		public function showPanelEx(titleStr		:String,
								  messageStr		:String,	
								  buttonStr			:Array,
								  buttonMode	:int		=	AlertUI.OK,
								  clickCallBack	:Function	=	null,
								  clickData		:Array		=	null,				
								  isInput		:Boolean	= 	false,
								  initStr:String = ""):void
		{
			clearPanel();
			visible = true;
			title	=	titleStr;
			titleSize	=	this.width + 12;
//			titleSize	+=	94;
			callBackFn	=	clickCallBack;
			if(isInput)
			{
				drawInputStr();
				inputTxt.text = initStr;
				inputTxt.restrict = null;
				if( _isHideMsg )
				{
					inputTxt.addEventListener( MouseEvent.CLICK, onInputTextClick );
				}
			}
			drawtxtNormal(messageStr,isInput);
			
			centerButtonPrint(buttonMode);
			
			_clickData	=	clickData;
			
			centerButtonStr(buttonMode,buttonStr);
			
			center();
		}
		
		public function showPanel(titleStr		:String,
								  messageStr		:String,	
								  buttonMode	:int		=	AlertUI.OK,
								  clickCallBack	:Function	=	null,
								  clickData		:Array		=	null,				
								  isInput		:Boolean	= 	false,initStr:String = "",restrictStr:String = null,
								  firstLabel	:String		=	null,secondLabel	:String	=	null):void
		{
			clearPanel();
			visible = true;
			title	=	titleStr;
			titleSize	=	this.width + 12;
//			titleSize	+=	94;
			callBackFn	=	clickCallBack;
			if(isInput)
			{
				inputTxt.restrict = restrictStr;

				drawInputStr();
				inputTxt.text = initStr;
			}
			drawtxtNormal(messageStr,isInput);
			centerButtonPrint(buttonMode,firstLabel,secondLabel);
			
			_clickData	=	clickData;
			
			center();
		}
		
		private function center():void{
			var xPoint:int = (StageInfo.stage.stageWidth-notePanel.width)/2 ;
			var yPoint:int = (StageInfo.stage.stageHeight-notePanel.height)/2 ;
			notePanel.x	= xPoint;
			notePanel.y	= yPoint;
		}
		
		private function drawInputStr():void
		{
			_inputSpace.x	=	width/4;
			_inputSpace.y	=	(height-_inputSpace.height)/2.5 + 10;
			_inputSpace.visible	=	true;
		}
		
		private function drawtxtNormal(str:String,isInput:Boolean):void{
			txtNormal.width		=	width*3/4;
			txtNormal.htmlText	=	str;
			txtNormal.height = txtNormal.textHeight+10;
			var txtData:Bitmap	=	DrawAPI.getLabelBitmap(txtNormal);
			_normalSpace.addChild(txtData);
			if((_normalSpace.height+90)<150){
				height = 150
			}else{
				height = _normalSpace.height+120
			}
			_height = height;
			var tempHeight:int = (_normalSpace.height+90)<150?150:_normalSpace.height+120;
			drawNormalPoint(isInput,txtData);
		}
		
		private function drawNormalPoint(isInput:Boolean,txtData:Bitmap):void
		{
			if(!isInput){
				_normalSpace.x	=	(width-txtData.width)/2;
				_normalSpace.y	=	(height-txtData.height)/2-20;
			}else{
				_normalSpace.x	=	this.width/2 - _normalSpace.width/2+10;
				_normalSpace.y	=	(height-txtData.height)/2-30;
			}
		}
		
		private function clearPanel():void
		{
			while(_normalSpace.numChildren>0)
			{
				_normalSpace.removeChildAt(0);
			} 
			while(_buttonSpace.numChildren>0)
			{
				_buttonSpace.removeChildAt(0);
			}
			inputTxt.text	=	"";
			_inputSpace.visible	=	false;
			
			width = 300;
			height = 150;
		}
		private function getStr(mode:int):String
		{
			return StaticIni.getSwfLangStr("AlertEx_"+mode);	
		}
		
		/**
 		 *添加按钮到容器中 
 		 */		
		private function centerButtonPrint(mode:int,firstLabel:String=null,secondLabel:String=null):void
		{
			 var btnCount		:int		=	0;
			 if( YES & mode)
			 {				
				 firstBtn.label	=	firstLabel==null?getStr(AlertUI.YES):firstLabel;
				 firstBtn.name	=	AlertUI.YES.toString();
				 firstBtn.show(_buttonSpace);
				 firstBtn.addEventListener(MouseEvent.MOUSE_UP, onButtonClick);
				 btnCount++;
			 }
			 
			 if( NO & mode)
			 {				
				 secondBtn.label	=	secondLabel==null?getStr(AlertUI.NO):secondLabel;
				 secondBtn.name	=	AlertUI.NO.toString();
				 secondBtn.show(_buttonSpace);
				 secondBtn.addEventListener(MouseEvent.MOUSE_UP, onButtonClick);
				 
				 secondBtn.x	=	firstBtn.x	+	(firstBtn.width	+	_distace) * btnCount;
				 
				 btnCount++;
			 }
			 
			 if( OK & mode)
			 {				
				 thirdBtn.label	=	firstLabel==null?getStr(AlertUI.OK):firstLabel;
				 thirdBtn.name	=	AlertUI.OK.toString();
				 thirdBtn.show(_buttonSpace);
				 thirdBtn.addEventListener(MouseEvent.MOUSE_UP, onButtonClick);
				 
				 thirdBtn.x	=	firstBtn.x	+	(firstBtn.width	+	_distace) * btnCount;
				 btnCount++;
			 }
			 
			 if( CANCEL & mode)
			 {				
				 fouthBtn.label	=	secondLabel==null?getStr(AlertUI.CANCEL):secondLabel;
				 fouthBtn.name	=	AlertUI.CANCEL.toString();
				 fouthBtn.show(_buttonSpace);
				 fouthBtn.addEventListener(MouseEvent.MOUSE_UP, onButtonClick);
				 
				 fouthBtn.x	=	firstBtn.x	+	(firstBtn.width	+	_distace) * btnCount;
				 btnCount++;
			 }
			setButtonXY();
		}
		
		/**
		 *添加按钮到容器中 
		 */		
		private function centerButtonStr(mode:int,strP:Array):void
		{
			if(!strP || 0 == strP.length)
			{
				return;
			}
			
			var btnCount:int	=	0;
			if( YES & mode)
			{				
				firstBtn.label	=	strP[0];
			}
			
			if( NO & mode)
			{				
				secondBtn.label	=	strP[1];
			}
			
			if( OK & mode)
			{				
				thirdBtn.label	=	strP[0];
			}
			
			if( CANCEL & mode)
			{				
				fouthBtn.label	=	strP[1];
			}
		}
		
		private function onButtonClick(e:MouseEvent):void
		{
			this.visible	=	false;
			_isHideMsg = false;
			deleteEvent();
		
			{		if(callBackFn is Function)			
				callBackFn.call(this,e.target.name,inputTxt.text,_clickData);
			}
		}
		
		private function onInputTextClick(e:MouseEvent):void
		{
			inputTxt.removeEventListener( MouseEvent.CLICK, onInputTextClick );
			
			inputTxt.text = "";
		}
		
		public function get buttonFlag():uint
		{
			return _buttonMode;
		}

		public function set buttonFlag(value:uint):void
		{
			_buttonMode = value;
		}

	}
}